package fzu.functiongather.Utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

import fzu.functiongather.R;

/**
 * Created by WLT on 2016/7/13.
 */
public class HorizontalProgressbar extends ProgressBar{
    private static final int DEFAULT_TEXT_SIZE=10;//sp
    private static final int DEFAULT_TEXT_COLOR=0xFFFC00D1;
    private static final int DEFAULT_COLOR_UNREACH=0xFFD3D6D7;
    private static final int DEFAULT_HIGHT_UNREACH=2;//dp
    private static final int DEFAULT_COLOR_REACH=DEFAULT_TEXT_COLOR;//sp
    private static final int DEFAULT_HIGHT_REACH=2;//dp
    private static final int DEFAULT_TEXT_OFFSET=10;//dp

    private int mTextSize=sp2px(DEFAULT_TEXT_SIZE);
    private int mTextColor=DEFAULT_TEXT_COLOR;
    private int mUnReachColor=DEFAULT_COLOR_UNREACH;
    private int mUnReachHeight=dp2px(DEFAULT_HIGHT_UNREACH);
    private int mReachColor=DEFAULT_COLOR_REACH;
    private int mReachHeight=dp2px(DEFAULT_HIGHT_REACH);
    private int mTextOffset=dp2px(DEFAULT_TEXT_OFFSET);

    private Paint mPaint=new Paint();//画笔
    private int  mRealWidth;//宽度


    public HorizontalProgressbar(Context context) {
        this(context, null);
    }

    public HorizontalProgressbar(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public HorizontalProgressbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyledAttrs(attrs);
    }


    /*
    * 获取自定义属性
    * */
    private void obtainStyledAttrs(AttributeSet attrs) {
        TypedArray ta=getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressbar);

        mTextSize=(int)ta.getDimension(R.styleable.HorizontalProgressbar_progress_text_size,mTextSize);
        mTextColor=ta.getColor(R.styleable.HorizontalProgressbar_progress_text_color,mTextColor);
        mTextOffset=(int)ta.getDimension(R.styleable.HorizontalProgressbar_progress_text_offset,mTextOffset);
        mUnReachColor=ta.getColor(R.styleable.HorizontalProgressbar_progress_unreach_color,mUnReachColor);
        mUnReachHeight=(int)ta.getDimension(R.styleable.HorizontalProgressbar_progress_unreach_height,mUnReachHeight);
        mReachColor=ta.getColor(R.styleable.HorizontalProgressbar_progress_reach_color, mReachColor);
        mReachHeight=(int)ta.getDimension(R.styleable.HorizontalProgressbar_progress_reach_height,mReachHeight);

        ta.recycle();
        mPaint.setTextSize(mTextSize);//text字体大小
    }

    /*
    * 测量
    * */
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     //  int widthMode=MeasureSpec.getMode(widthMeasureSpec);//宽度模式
        int widthVal=MeasureSpec.getSize(widthMeasureSpec);//宽度的值，用户所给默认值
        int height=MeasuredHeight(heightMeasureSpec);
        setMeasuredDimension(widthVal,height);
        mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
    }

    /*
    * 测量高度
    * */
    private int MeasuredHeight(int heightMeasureSpec) {
        int result=0;
        int mode=MeasureSpec.getMode(heightMeasureSpec);
        int size=MeasureSpec.getSize(heightMeasureSpec);

        //与精确值比较
        if(mode==MeasureSpec.EXACTLY){
            result=size;
        }else
        {
            int textHeight=(int)(mPaint.descent()-mPaint.ascent());//text的高度
            result =getPaddingBottom()+getPaddingTop()+Math.max(Math.max(mReachHeight,mUnReachHeight),Math.abs(textHeight));


          //测量值不能超过所给size
            if(mode==MeasureSpec.AT_MOST)
            {
                result=Math.min(result, size);
            }
        }
        return result;
    }

    /*
    * 画进度条
    * */
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        canvas.translate(getPaddingLeft(),getHeight()/2);

        //是否需要画未完成进度
        boolean noNeedUnRech=false;

        //画已完成进度
        String text=getProgress()+"%";
        int textWidth=(int)mPaint.measureText(text);
        float radio =getProgress()*1.0f/getMax();
        float progressX=radio*mRealWidth;
        if(progressX+textWidth>mRealWidth){
            progressX=mRealWidth-textWidth;
            noNeedUnRech=true;
        }
        float endX=progressX-mTextOffset/2;
        if(endX>0)
        {
            mPaint.setColor(mReachColor);
            mPaint.setStrokeWidth(mReachHeight);
            canvas.drawLine(0,0,endX,0,mPaint);//（startX,startY,endX,endY,Paint)

        }

        //画进度text
        mPaint.setColor(mTextColor);
        int y= (int)(-(mPaint.descent()+mPaint.ascent()/2));
        canvas.drawText(text,progressX,y,mPaint);

        //画未完成进度
        if(!noNeedUnRech)
        {
            float start=progressX+mTextOffset/2+textWidth;
            mPaint.setColor(mUnReachColor);
            mPaint.setStrokeWidth(mUnReachHeight);
            canvas.drawLine(start,0,mRealWidth,0,mPaint);
        }
        canvas.restore();
    }

    /*
    * dp到px的转化
    * */
    private  int dp2px(int dpVal)
    {
        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal,getResources().getDisplayMetrics());
    }

    /*
    * sp到px的转化
    * */
    private int sp2px(int spVal)
    {
        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getDisplayMetrics());
    }



}
